import { createRoot } from "react-dom/client";
// import './contents/jinritemaiGetProduct/index'
import "@Styles/doudianstyle.scss"
// import './store'
// import '@Utils/momentZhCN.js';

// const modules = import.meta.glob('./contents/*/index.tsx', { eager: true })
const modules = import.meta.glob('./1688Contents/*/index.tsx', { eager: true })
// const contentList = [jinritemai, jinritemaiCopyGoods]
const domModous = []

for (const content in modules) {
    console.log(modules[content])
    domModous.push(modules[content])
}

function attachShadow(element, content) {
    const anchor = {
        element,
    }
    const shadowHost = content.rendenRootDom && content.rendenRootDom() ||  document.createElement('div');
    content.getShadowHostId && shadowHost.setAttribute('id', content.getShadowHostId())

    // shadowHost.innerHTML = '1234567987'
    // 创建并附加 Shadow DOM
    // const shadowRoot = shadowHost.attachShadow({ mode: 'open' });
    if (!content.mountShadowHost) {
        element.insertAdjacentElement('afterend', shadowHost)
        // return
    } else {
        content.mountShadowHost({
            shadowHost,
            anchor
        })
    }


    const C = content.default
    createRoot(shadowHost).render(<C anchor={anchor} />)
    // shadowRoot.appendChild(content.getStyle())
}
domModous.forEach(content => {
    console.log('121456', content, location.href)
    let sendStatus = false
    if (content.config.matches) {
        content.config.matches.forEach((url) => {
            const Rex = new RegExp(url)
            console.log('Rex.test(location.href)', Rex.test(location.href))
            if (Rex.test(location.href)) {
                sendStatus = true
            }
        })
        // sendStatus = content.matchUrl()
    } else {
        sendStatus = true
    }
    console.log('sendStatus', sendStatus)
    if(!sendStatus) return
    // if (!content.matchUrl()) {
    //     return;
    // }
    if ('render' in content) {
        void content.render({
            createRootContainer: content.getRootContainer
        })
        return
    }

    if ('getInlineAnchorList' in content) {
        setInterval(() => {
            content.getInlineAnchorList().then((anchorList) => {
                // console.log('anchorList', anchorList)
                anchorList && anchorList.forEach((element) => {
                    if (element && !isInserted(element)) {
                        element.setAttribute('data-inserted', 'true');
                        attachShadow(element, content)
                    }
                })
            })
        }, 172)

    }
    if ('getInlineAnchor' in content) {
        setInterval(() => {
            content.getInlineAnchor().then((element) => {
                if (element && !isInserted(element)) {
                    element.setAttribute('data-inserted', 'true');
                    attachShadow(element, content)
                }
            })
        }, 172)

    }

})
function isInserted(element) {
    return element.getAttribute('data-inserted') === 'true';
}
